/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.CRM.bean.SalesOrderProcessingModuleRemote;
import Merlion.CRM.entity.PricebookEntity;
import Merlion.util.state.PricebookEntryState;
import Merlion.util.state.PricebookState;
import Merlion.util.state.ProductState;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;


@ManagedBean(name = "PricebookManagedBean")
@ViewScoped
public class PricebookManagedBean {

    private List<ProductState> productList;
    private ProductState selectedProduct;
    private List<PricebookState> pricebookList = new ArrayList<PricebookState>();
    private PricebookState selectedPricebook = new PricebookState();
    private PricebookState newPricebook;
    private Long productId;
    private List<PricebookEntryState> entryList = new ArrayList<PricebookEntryState>();
    private List<PricebookEntryState> newEntryList = new ArrayList<PricebookEntryState>();
    private PricebookEntryState newEntry = new PricebookEntryState();
    private PricebookEntryState selectedPricebookEntry;
    private Long pricebookId;
    private String pricebookName;
    private Long deletedPricebookId;

    /** Creates a new instance of PricebookManagedBean */
    public PricebookManagedBean() {
    }
    @EJB
    private SalesOrderProcessingModuleRemote sop;

    public void initView(PhaseEvent event) {
        if (entryList == null) {
            entryList = new ArrayList<PricebookEntryState>();
        }
    }

    public Long getPricebookId() {
        return pricebookId;
    }

    public void setPricebookId(Long pricebookId) {
        System.out.println("SET SELECTED PRICEBOOK ID");
        this.pricebookId = pricebookId;
    }

    public void handleProductChange() {
        if (productId != null) {
            System.out.println("Handle product change :: productId not null");
            List<PricebookState> list = sop.getPricebookState(productId);
            this.setPricebookList(list);
        }
    }

    public void handlePricebookChange() {
        if (pricebookId != null) {
            System.out.println("Handle pricebook change :: pricebookId not null");
            List<PricebookEntryState> prState = sop.getPricebookEntry(pricebookId);
            System.out.println("Selected pricebook not null!");
            //List<PricebookEntryState> prState = selectedPricebook.getPricebook();
            this.setEntryList(prState);
        }
    }

    public void onEditRowPricebook(RowEditEvent event) {
        System.out.println(":::WEB::: On Edit Row");
        PricebookEntryState entry = (PricebookEntryState) event.getObject();

        int low = entry.getLow();
        int high = entry.getHigh();
        double directPrice = entry.getDirectPrice();
        double wholesalePrice = entry.getWholesalePrice();
        System.out.println("Low : " + low + " High : " + high + "Direct Price : " + directPrice + " wholesalePrice : " + wholesalePrice);
        Long ePricebookId = entry.getPricebookId();
        int number = entry.getNumber();

        sop.editPricebookEntry(ePricebookId, number, low, high, directPrice, wholesalePrice);
    }

    public void delete(ActionEvent event) {
        System.out.println("Delete pricebook");
        if (deletedPricebookId != null) {
            System.out.println("Deleted pricebook Id not null");
            sop.deletePricebook(deletedPricebookId);
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("pricebooks.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void handleDelete() {
        System.out.println("Handle delete");
        if (deletedPricebookId != null) {
            System.out.println("set deleted id");
            this.setDeletedPricebookId(deletedPricebookId);
        }
    }

    public PricebookEntryState getSelectedPricebookEntry() {
        return selectedPricebookEntry;
    }

    public void setSelectedPricebookEntry(PricebookEntryState selectedPricebookEntry) {
        this.selectedPricebookEntry = selectedPricebookEntry;
    }

    public List<PricebookEntryState> getEntryList() {
        return entryList;
    }

    public void setEntryList(List<PricebookEntryState> entryList) {
        System.out.println("Set entry List");
        this.entryList = entryList;
    }

    public Long getProductId() {
        return productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public String getPricebookName() {
        return pricebookName;
    }

    public void setPricebookName(String pricebookName) {
        this.pricebookName = pricebookName;
    }

    public PricebookState getNewPricebook() {
        return newPricebook;
    }

    public void setNewPricebook(PricebookState newPricebook) {
        this.newPricebook = newPricebook;
    }

    public List<PricebookState> getPricebookList() {
        if (pricebookList == null) {
            pricebookList = sop.getPricebookStateList();
        }
        return pricebookList;
    }

    public void setPricebookList(List<PricebookState> pricebookList) {
        this.pricebookList = pricebookList;
    }

    public List<ProductState> getProductList() {
        if (productList == null) {
            productList = sop.viewProductList();
        }
        return productList;
    }

    public void setProductList(List<ProductState> productList) {
        this.productList = productList;
    }

    public PricebookState getSelectedPricebook() {
        return selectedPricebook;
    }

    public void setSelectedPricebook(PricebookState selectedPricebook) {
        System.out.println("Set Selected Pricebook");
        this.setEntryList(selectedPricebook.getPricebook());
        this.selectedPricebook = selectedPricebook;
    }

    public ProductState getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(ProductState selectedProduct) {
        this.selectedProduct = selectedProduct;
    }

    public PricebookEntryState getNewEntry() {
        return newEntry;
    }

    public void setNewEntry(PricebookEntryState newEntry) {
        this.newEntry = newEntry;
    }

    public List<PricebookEntryState> getNewEntryList() {
        return newEntryList;
    }

    public void setNewEntryList(List<PricebookEntryState> newEntryList) {
        this.newEntryList = newEntryList;
    }

    public Long getDeletedPricebookId() {
        return deletedPricebookId;
    }

    public void setDeletedPricebookId(Long deletedPricebookId) {
        this.deletedPricebookId = deletedPricebookId;
    }

    public String reinit() {

        newEntry = new PricebookEntryState();

        return null;
    }

    public void addPricebook(ActionEvent event) {
        System.out.println("Add pricebook ");
        if (productId != null) {
            System.out.println("Product not null, calling add pricebook!");
            sop.createPricebook2(pricebookName, productId, newEntryList);
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("pricebooks.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
